package ai.zixing.mashibing.basic_class.class08;

import java.util.LinkedList;

// 是否为完全二叉树
public class Code06_IsCBT {

    public static boolean isCBT1(Node head) {
        if (head == null) {
            return true;
        }
        LinkedList<Node> queue = new LinkedList<>();
        // 是否遇到左右两孩子不双全的节点
        boolean leaf = false;
        Node l = null;
        Node r = null;
        queue.add(head);
        while (!queue.isEmpty()) {
            head = queue.poll();
            l = head.left;
            r = head.right;
            // 如果遇到不双全的节点，且不是叶子节点，则不是完全二叉树
            if ((leaf && !(l == null && r == null)) || (l == null && r != null)) {
                return false;
            }
            if (l != null) {
                queue.add(l);
            }
            if (r != null) {
                queue.add(r);
            }
            // 有两个孩子不为空则为 true
            if (l == null || r == null) {
                leaf = true;
            }
        }
        return true;
    }

    public static class Info{
        // 是否满二叉树
        public boolean isFull;

        // 是否是二叉树
        public boolean isCBT;

        // 树的高度
        public int heigth;

        public Info(boolean isFull, boolean isCBT, int heigth) {
            this.isFull = isFull;
            this.isCBT = isCBT;
            this.heigth = heigth;
        }
    }

    public static boolean isCBT2(Node head) {
        if (head == null) {
            return true;
        }
        return process(head).isCBT;
    }


    public static Info process(Node head) {
        if (head == null) {
            return new Info(true, true, 0);
        }
        Info leftInfo = process(head.left);
        Info rightInfo = process(head.right);
        int height = Math.max(leftInfo.heigth, rightInfo.heigth) + 1;
        boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.heigth == rightInfo.heigth;
        boolean isCBT = false;
        if (isFull) {
            isCBT = true;
        } else {
            if (leftInfo.isCBT && rightInfo.isCBT) {
                // 左树是满树、右树是完全二叉树，且右树 + 1 == 左树高度
                if (leftInfo.isCBT && rightInfo.isFull && leftInfo.heigth == rightInfo.heigth + 1) {
                    isCBT = true;
                }
                if (leftInfo.isFull && rightInfo.isFull && leftInfo.heigth == rightInfo.heigth + 1) {
                    isCBT = true;
                }
                if (leftInfo.isFull && rightInfo.isCBT && leftInfo.heigth == rightInfo.heigth) {
                    isCBT = true;
                }
            }
        }
        return new Info(isFull, isCBT, height);
    }

    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    public static void main(String[] args) {
        int maxLevel = 5;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            if (isCBT1(head) != isCBT2(head)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }
}
